package edu.ues21.tcs.ecommerce2011.mvc.controller;

import java.io.IOException;
import java.sql.Date;

import java.text.SimpleDateFormat;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import edu.ues21.tcs.ecommerce2011.mvc.dao.ListaPrecioDAO;
import edu.ues21.tcs.ecommerce2011.mvc.model.ListaPrecio;

/**
 * 
 * @author Acunia Marco.
 */
public class AddListaPrecioController extends HttpServlet {

	ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
			"/BeanLocations.xml");

	ListaPrecioDAO mDao = (ListaPrecioDAO) appContext.getBean("listaprecioDAO");

	private static final long serialVersionUID = 1L;

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("view/AddListaPrecio.jsp").forward(
				request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String fechaDesdeStr = request.getParameter("desde");
		String fechaHastaStr = request.getParameter("hasta");
		String nombre = request.getParameter("nombre");
		
		SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd", new Locale("es", "ES"));
		System.out.println("++++++++++++++++++++++++++++ ");

		if (fechaDesdeStr != null && fechaHastaStr != null) {
			ListaPrecio l = new ListaPrecio();
			try{
			Date fechaDesde = new java.sql.Date(sdf.parse(fechaDesdeStr).getTime());
			Date fechaHasta = new java.sql.Date(sdf.parse(fechaHastaStr).getTime());
			
			l.setFechaDesde(fechaDesde);
			l.setFechaHasta(fechaHasta);
			l.setNombre(nombre);
			}catch(Exception e){}
			mDao.save(l);
		}

		response.sendRedirect("listarLP.do");

	}

	@Override
	public String getServletInfo() {
		return "Short description";
	}// </editor-fold>

}
